//// texture.cpp  demonstrates methods using bitmap files
////to draw predefined images on the our objects.
//#include <windows.h>
//#include <glut.h>
//#include <stdio.h>
//// Prototypes
//void display();
//void animation();
//void polygon(int a,int b, int c,int d);
//void cube();
//void myinit();
//int LoadBitmap( char *filename ) ;
//// Global variables
//GLint  winWidth = 640, winHeight = 480;
//GLint  num_texture = -1;// The counter of the current texture => id
//double rotation=0, rotation_increment=0.1;
//GLint			id_texture;
//GLfloat vertices[][3]={{-10.,  -10.,  -10.},{ 10.,  -10.,  -10.},  
//				 { 10.,   10.,  -10.},{-10.,   10.,  -10.},   
//				 {-10.,  -10.,   10.},{ 10.,  -10.,   10.},   
//				 { 10.,   10.,   10.},{-10.,   10.,   10.} };
//void main(int argc, char **argv)
//{
//    glutInit( &argc, argv );    
//    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
//    glutInitWindowSize( winWidth, winHeight );
//    glutInitWindowPosition( 100, 100 );
//    glutCreateWindow("Cube with texture");    
//    glutDisplayFunc( display );
//    glutIdleFunc( animation );
//    myinit();
//    glutMainLoop();
//}
//void  myinit()
//{
//    glClearColor( 0.65, 0.8, 0.1, 1.0);	
//    // Viewport transformation
//    glViewport( 0, 0, winWidth, winHeight );  
//    // Projection transformation
//    glMatrixMode(GL_PROJECTION);
//    glLoadIdentity();
//    gluPerspective( 45.0,(GLfloat)winWidth/(GLfloat)winHeight,
//	1.0, 1000.0);
//    glMatrixMode(GL_MODELVIEW);
//    glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
//    glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
//    id_texture = LoadBitmap("Giraffe.bmp"); // The Function LoadBitmap() return the current texture ID
//}
//
//void animation()
//{
//    rotation = rotation + rotation_increment;
//    if( rotation > 360. ) rotation-= 360.;
//	glutPostRedisplay();
//}
//void display()
//{
//    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//    glLoadIdentity(); // Initialize the model matrix as identity
//    glTranslatef(0.0,0.0,-50);
//    glRotatef(rotation,1.0,1.0,1.0);
//glBindTexture( GL_TEXTURE_2D,id_texture ); // We set the active texture 
//
//    cube();
//    glFlush();
//    glutSwapBuffers();
//}
//
//void polygon(int a,int b, int c,int d)
//{
//	glBegin(GL_POLYGON);
//	glTexCoord2f(0.,0.);
//	glVertex3fv(vertices[a]);
//	glTexCoord2f(0.,1.);
//	glVertex3fv(vertices[b]);
//	glTexCoord2f(1.,1.);
//	glVertex3fv(vertices[c]);
//	glTexCoord2f(1.,0.);
//	glVertex3fv(vertices[d]);
//	glEnd();
//}
//void cube()
//{
//	polygon(0,3,2,1);
//	polygon(2,3,7,6);
//	polygon(0,4,7,3);
//	polygon(1,2,6,5);
//	polygon(4,5,6,7);
//	polygon(0,1,5,4);
//}
//int LoadBitmap(char *filename) 
//{
//    // windows.h gives us these types to work with the Bitmap files
//    BITMAPFILEHEADER fileheader; 
//    BITMAPINFOHEADER infoheader;
//    RGBTRIPLE rgb;
//
//    num_texture ++; // The counter of the current texture is increased
//    FILE *pFile = fopen( filename, "rb"); // Open the file for reading
//    if( NULL == pFile ) return (-1);	
//    fread( &fileheader, sizeof(fileheader), 1, pFile); // Read the fileheader
//    fseek( pFile, sizeof(fileheader), SEEK_SET); // Jump the fileheader
//    fread( &infoheader, sizeof(infoheader), 1, pFile); // and read the infoheader
//    // Now we need to allocate the memory for our image (width * height * color deep)
//	int nSize = infoheader.biWidth * infoheader.biHeight * 4;
//    unsigned char *pTexture = (unsigned char *)new char[nSize];
//    // And fill it with zeros
//	ZeroMemory( pTexture, sizeof( char ) * nSize );
//    // At this point we can read every pixel of the image
//	int j = 0;
//    for(int i=0; i<infoheader.biWidth * infoheader.biHeight; i ++ )
//    {            
//            // We load an RGB value from the file
//            fread( &rgb, sizeof(rgb), 1, pFile); 
//            // And store it
//            pTexture[j+0] = rgb.rgbtRed;	// Red component
//            pTexture[j+1] = rgb.rgbtGreen;	// Green component
//            pTexture[j+2] = rgb.rgbtBlue;	// Blue component
//            pTexture[j+3] = 255;			// Alpha value
//            j += 4; // Go to the next position
//    }
//    fclose( pFile ); // Closes the file stream
//     
//    glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter
//    // The next commands sets the texture parameters
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
//    // Finally we define the 2D texture
//    glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pTexture);
//    // And create 2d mipmaps for the minifying function
//    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, pTexture);
//
//
//	delete[] pTexture;  // Free the memory we used to load the texture
//    return num_texture; // Returns the current texture OpenGL ID
//}
